package repository

import (
	v1 "server_go/api/v1"
	"server_go/internal/model"
)

type TagRepository interface {
	GetTagByName(tagName string) (*model.Tag, error)
	GetTagById(tagId int64) (*model.Tag, error)
	AddTag(tag *model.Tag) error
	DeleteTag(tagId int64) error
	UpdateTag(tagId int64, tag *model.Tag) error
	GetAllTag(page, pageSize int) ([]*model.Tag, int64, error)
	GetAllTagList() ([]*model.Tag, int64, error)
}

func NewTagRepository(r *Repository) TagRepository {
	return &tagRepository{
		Repository: r,
	}
}

type tagRepository struct {
	*Repository
}

func (r *tagRepository) GetTagByName(tagName string) (*model.Tag, error) {
	// 计数
	var count int64
	if err := r.db.Table("tag").
		Where("name = ? AND deleted_at IS NULL", tagName).
		Count(&count).Error; err != nil {
		return nil, err
	}
	// 判断标签是否存在
	if count == 0 {
		return nil, v1.ErrTagNotExist
	}
	var tag *model.Tag
	if err := r.db.Table("tag").Where("name = ?", tagName).First(&tag).Error; err != nil {
		return nil, err
	}
	return tag, nil
}

func (r *tagRepository) GetTagById(tagId int64) (*model.Tag, error) {
	// 计数
	var count int64
	if err := r.db.Table("tag").
		Where("id = ? AND deleted_at IS NULL", tagId).
		Count(&count).Error; err != nil {
		return nil, err
	}
	// 判断标签是否存在
	if count == 0 {
		return nil, v1.ErrTagNotExist
	}
	var tag *model.Tag
	if err := r.db.Table("tag").Where("id = ?", tagId).First(&tag).Error; err != nil {
		return nil, err
	}
	return tag, nil
}

func (r *tagRepository) AddTag(tag *model.Tag) error {
	// 创建
	if err := r.db.Table("tag").Create(tag).Error; err != nil {
		return err
	}
	return nil
}

func (r *tagRepository) DeleteTag(tagId int64) error {
	// r.db.Delete(&model.Tag{}, tagId): 根据主键删除
	if err := r.db.Delete(&model.Tag{}, tagId).Error; err != nil {
		return err
	}
	return nil
}

func (r *tagRepository) UpdateTag(tagId int64, tag *model.Tag) error {
	// 设置更新参数
	updates := make(map[string]interface{})
	if tag.Name != "" {
		updates["name"] = tag.Name
	}
	if tag.Describe != "" {
		updates["describe"] = tag.Describe
	}
	// 更新tag的内容
	if err := r.db.Table("tag").Where("id = ?", tagId).Updates(updates).Error; err != nil {
		return err
	}
	return nil
}

func (r *tagRepository) GetAllTag(page, pageSize int) ([]*model.Tag, int64, error) {
	tags := make([]*model.Tag, 0)
	offset := (page - 1) * pageSize
	// 查询表中数据分页查询
	if err := r.db.Table("tag").
		//Order("updated_at desc"). // 根据更新时间排序
		Limit(pageSize).
		Offset(offset).
		Find(&tags).Error; err != nil {
		return nil, 0, err
	}
	var total int64
	// 统计总数
	if err := r.db.Table("tag").
		Where("deleted_at IS NULL").
		Count(&total).Error; err != nil {
		return nil, 0, err
	}
	//返回结果
	return tags, total, nil
}

func (r *tagRepository) GetAllTagList() ([]*model.Tag, int64, error) {
	tags := make([]*model.Tag, 0)
	if err := r.db.Table("tag").
		Where("deleted_at IS NULL").
		Find(&tags).Error; err != nil {
		return nil, 0, err
	}
	var total int64
	if err := r.db.Table("tag").
		Where("deleted_at IS NULL").
		Count(&total).Error; err != nil {
		return nil, 0, err
	}
	return tags, total, nil
}
